package script;


import com.zh.freechat.domain.user.service.UserContactsService;
import com.zh.webcommon.common.support.SpringContextHolder;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

import static com.zh.webcommon.CommonConst.GSON;

/**
 * 添加好友的脚本
 * 仅用于表数据为数据时
 * @author Zhou
 * @date 21:38 2024/4/8
 */
public class AddFriendsScript {
    static Random random = new Random();

    static String[] allUserIds;

    public static void InitSetUserIds() {
        // select uid from ch_user where is_robot=0;
        allUserIds = new String[]{
                "ch_002e5a3de4b8",
                "ch_00aa0790d110",
                "ch_02949872d88f",
                "ch_0376ad0ee5ff",
                "ch_03e5eff8a91c",
                "ch_05adf540b5b4",
                "ch_06acad5a43ed",
                "ch_0891be767a1c",
                "ch_08b0b205af35",
                "ch_09ea48656028",
                "ch_0c2cfd7d6e13",
                "ch_0e47fb7ebe13",
                "ch_0e66bfb33bd0",
                "ch_108b2a63268d",
                "ch_113220e572c1",
                "ch_1169567341e8",
                "ch_15625f86e09b",
                "ch_1569426592f8",
                "ch_158e97a8b7e5",
                "ch_181c75e05ff2",
                "ch_185238548ab7",
                "ch_19128e354d6a",
                "ch_1ac3d2e3e3df",
                "ch_1b28112f9526",
                "ch_1bcebc476c09",
                "ch_1bf427275ed4",
                "ch_1ec9d5e75f26",
                "ch_2105af297ad1",
                "ch_22b667d80816",
                "ch_22d841a10b82",
                "ch_237829fc98b8",
                "ch_247d11d294cb",
                "ch_24e5c1f4079b",
                "ch_26286a1dfd39",
                "ch_269b87e106f6",
                "ch_26a07a0905e0",
                "ch_273e94f81515",
                "ch_28479b4a39e3",
                "ch_28483bc4e1e1",
                "ch_28e53162f6fc",
                "ch_296e97915583",
                "ch_2b98d7dcb4bd",
                "ch_2c012ddce894",
                "ch_2c22219c4f00",
                "ch_2c67d8135967",
                "ch_2dd6aaba6a48",
                "ch_2e6414f19d36",
                "ch_308e7f9ee8b6",
                "ch_3143f0bc15ab",
                "ch_315155fbd32e",
                "ch_33293cec0f3d",
                "ch_332cba36c0b4",
                "ch_355603014ffd",
                "ch_35e7a37989a8",
                "ch_371e262b22b9",
                "ch_371e4b0f9971",
                "ch_3739ac5335ee",
                "ch_37ccb17b9a0b",
                "ch_3dfd75c6e74a",
                "ch_4100e67e04a1",
                "ch_411246ffd045",
                "ch_4176e983a561",
                "ch_42a98c8c887c",
                "ch_4394d037f639",
                "ch_44a63d902aca",
                "ch_456a69be2695",
                "ch_46834128fab7",
                "ch_47ea054a9d1d",
                "ch_48113e889db1",
                "ch_4829f8642e4f",
                "ch_49f98a2ef63d",
                "ch_4a5cd3cd6f63",
                "ch_4aedbfdfc3f9",
                "ch_4be8cc40921b",
                "ch_4de2352bb128",
                "ch_4e2e4abe7d67",
                "ch_4ebadcbe2f30",
                "ch_4efb61473d82",
                "ch_4f13bdc9bb74",
                "ch_4f3621730455",
                "ch_4fa11235b772",
                "ch_4fc082887d08",
                "ch_525518474aa8",
                "ch_5399b5765fa6",
                "ch_549463fb739e",
                "ch_54fe1a7aca1a",
                "ch_564f5a4789b6",
                "ch_56cbe382496b",
                "ch_5a40110c84b9",
                "ch_5af73830c096",
                "ch_5b3e374529ab",
                "ch_5b6648487e21",
                "ch_5cde7995b053",
                "ch_5d80911746af",
                "ch_5f634fe7b7b5",
                "ch_5fa757b164d5",
                "ch_5fbbd280a73c",
                "ch_60141cde1b9e",
                "ch_60dacd922a33",
                "ch_62936c97bec1",
                "ch_63b92c18f537",
                "ch_6513b626b5d8",
                "ch_6600d0d6ef4f",
                "ch_66710bd635c7",
                "ch_6787fec6a0ca",
                "ch_68973ef1eedf",
                "ch_6a767e0ef1e7",
                "ch_6bc1f82a5906",
                "ch_6da206aea030",
                "ch_703f6d83c62a",
                "ch_7123a2ecffd8",
                "ch_79b145363650",
                "ch_7abe7bd36eb6",
                "ch_7be2cd8b6bc2",
                "ch_7c91de852acd",
                "ch_7cd5456fde5a",
                "ch_7dc542eaec2e",
                "ch_7dceae4e6009",
                "ch_7dec71b98c4e",
                "ch_7df323a28231",
                "ch_8102dcbd1091",
                "ch_82f10acf3b89",
                "ch_85e98a9be358",
                "ch_87ecd663f57a",
                "ch_8809dcb62df7",
                "ch_8889445f34ba",
                "ch_897a78720f47",
                "ch_8b65d695f2ad",
                "ch_8b7d32523e81",
                "ch_8cb22b0a44ce",
                "ch_8d6ceeda98e9",
                "ch_8d823b4d856b",
                "ch_8dceed7fe3c9",
                "ch_8e75c821fd65",
                "ch_8ef414538295",
                "ch_92c3e310553c",
                "ch_948e2eee4bbe",
                "ch_951764488857",
                "ch_95feb58e8f54",
                "ch_997b8823121e",
                "ch_9ff40888f201",
                "ch_a4374bebcf8d",
                "ch_a450f14b2643",
                "ch_a81d47ada607",
                "ch_a91e152e29de",
                "ch_aa817443e09c",
                "ch_acf329b91de5",
                "ch_af2b19aa2266",
                "ch_b0ed35852430",
                "ch_b14d3f4d348a",
                "ch_b199b2adb6df",
                "ch_b238b752e6e5",
                "ch_b2c09e06bc1d",
                "ch_b39ae0214f53",
                "ch_b47a12e59d7e",
                "ch_b5150be4febc",
                "ch_b556e7b624a1",
                "ch_b63fe9fe20e4",
                "ch_b7c8251b1263",
                "ch_b7f7b82686fc",
                "ch_b808e65b441c",
                "ch_bac94280e98b",
                "ch_bc16c7d27629",
                "ch_be8d236a186f",
                "ch_befa421a8fd8",
                "ch_c1da07892895",
                "ch_c267a9227527",
                "ch_c288090b3f7a",
                "ch_c67d7f7add6c",
                "ch_c68c371b2cc8",
                "ch_c94b611f90f2",
                "ch_c95c4529a9d4",
                "ch_caedf9de88ea",
                "ch_cdcb08973b3e",
                "ch_cdf6db09bc98",
                "ch_ce3cbba9bf38",
                "ch_cf22e2d202c2",
                "ch_cfe79290a083",
                "ch_d0632f3f57f8",
                "ch_d28fcf37b5a4",
                "ch_d2fdf65e3c80",
                "ch_d6d491e50e0a",
                "ch_d810c761585f",
                "ch_d9614b7d2fa6",
                "ch_d9db128bacb3",
                "ch_da48e8c367ee",
                "ch_dbbcdd1c83b2",
                "ch_dbea8818b5ef",
                "ch_dc06fbd6b771",
                "ch_dc9fd6aa3433",
                "ch_df26d4fbe96e",
                "ch_e0259fcff4fd",
                "ch_e0ae94054d6e",
                "ch_e1a8e6b6a5f6",
                "ch_e2126523758e",
                "ch_e3231f735a3d",
                "ch_e3933f5752eb",
                "ch_e3c80831cdda",
                "ch_e4ece9446497",
                "ch_e665401f1f2b",
                "ch_ea0da6c3a37f",
                "ch_eb1ccada6473",
                "ch_ebb39bafdafd",
                "ch_ecd64f1b5b24",
                "ch_edf7fb259d50",
                "ch_eec19c44d864",
                "ch_ef14b2b18622",
                "ch_f12bc4b10d22",
                "ch_f176fd81ed9f",
                "ch_f1f92becf022",
                "ch_f22111bb5b41",
                "ch_f268fffc7672",
                "ch_f36dada73715",
                "ch_fbd79fd37271",
                "ch_fe436be3247f",
                "ch_fec069be9f76",
                "ch_fff524692421",
                "ch_ffffffeeeeee",
        };

    }
    static {
        InitSetUserIds();
    }

    static List<String> getRandFriends(int N) {
        int allSize = allUserIds.length;
        if (N>allSize/3){
            throw new IllegalArgumentException("N 的值不能超过allSize的1/3");
        }
        List<String> friends = new ArrayList<>(N);
        for (int i = 0; i < N; i++) {
            friends.add(allUserIds[random.nextInt(allSize)]);
        }
        return friends;
    }

    /**
     * 申请好友
     */
    public static void applyFriends() {
        Map<String, List<String>> map = new HashMap<>();

        for (String allUserId : allUserIds) {
            map.put(allUserId, getRandFriends(10));
        }

        var userContactsService = SpringContextHolder.getBean(UserContactsService.class);

        map.forEach((currentUserId, ids) -> {
            for (String friendId : ids) {
                if (!Objects.equals(friendId.trim(), currentUserId)) {
                    try {
                        userContactsService.applyAdd(currentUserId, friendId, "我想加你");
                    } catch (Exception ex) {
                        System.out.println(ex.getMessage());
                    }
                }
            }

            userContactsService.addBlack(currentUserId,"ch_ffffffeeeeee");
        });
    }

    /**
     * 同意好友申请
     */
    public static void agreeApply() {
        var userContactsService = SpringContextHolder.getBean(UserContactsService.class);

        for (String userId : allUserIds) {
            var applyRecordList = userContactsService.findUnsettledApplyRecordList(userId);

            if (CollectionUtils.isNotEmpty(applyRecordList)) {

                applyRecordList.forEach(e -> {
                    try {
                        userContactsService.agreedFriend(e.getId(), e.getUserId());
                    } catch (Exception ex) {
                        System.out.println(ex.getMessage());
                    }
                });
                System.out.println("[userId=" + userId + "] 申请好友列表 = " + GSON.toJson(applyRecordList));
            }
        }

    }

    /**
     * 显示申请好友列表
     */
    public static void showApplyList() {
        var userContactsService = SpringContextHolder.getBean(UserContactsService.class);

        for (String userId : allUserIds) {
            var applyRecordList = userContactsService.findUnsettledApplyRecordList(userId);

            if (CollectionUtils.isNotEmpty(applyRecordList)) {
                System.out.println("[userId=" + userId + "] 申请好友列表 = " + GSON.toJson(applyRecordList));
            }
        }
    }

    /**
     * 显示好友列表
     */
    public static void showFriends() {
        var userContactsService = SpringContextHolder.getBean(UserContactsService.class);

        for (String userId : allUserIds) {
            var userContacts = userContactsService.contactsByUserId(userId).getFriends();

            if (CollectionUtils.isNotEmpty(userContacts)) {
                System.out.println("[userId=" + userId + "] 好友列表 = " + GSON.toJson(userContacts));
            }
        }

    }

}
